package com.yudean.esb.service.handler.impl;

import com.google.common.collect.Lists;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.yudean.esb.common.Constants;
import com.yudean.esb.dao.erp.*;
import com.yudean.esb.domain.erp.*;
import com.yudean.esb.service.handler.ReceiveErpDataService;
import com.yudean.esb.security.SecurtiyChecker;
import com.yudean.esb.util.StringUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

@Service("receiveErpDataServiceImpl")
@Scope("prototype")
public class ReceiveErpDataServiceImpl implements ReceiveErpDataService {

    private final static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReceiveErpDataServiceImpl.class);

    private String errorMsg = "";
    private String errorCode = "";
    private final String servicIDs = "erp_ids_contract,erp_ids_payments,ap_invoice,inv_ar,inv_ap,ar_invoice,ar_customers,general_ledger,ap_checks,ar_receipts,ap_receipts";

    //批量插入，一次批量插入20条数据
    private final int batchSize = 10;

    @Resource
    private SecurtiyChecker securtiyChecker;

    @Resource
    private ErpIdsApContractIntDao erpIdsApContractIntDao;
    @Resource
    private ErpIdsApConPaymentsIntDao erpIdsApConPaymentsIntDao;
    @Resource
    private ErpApInvoiceDao erpApInvoiceDao;
    @Resource
    private ErpApInvoiceLineDao erpApInvoiceLineDao;
    @Resource
    private ErpIdsInvArDao erpIdsInvArDao;
    @Resource
    private ErpIdsInvApLineDao erpIdsInvApLineDao;
    @Resource
    private ErpIdsInvApDao erpIdsInvApDao;
    @Resource
    private ErpIdsArCustomersDao erpIdsArCustomersDao;
    @Resource
    private ErpArInvoiceIntDao erpArInvoiceIntDao;
    @Resource
    private ErpIdsGLIntDao erpIdsGLIntDao;
    @Resource
    private ErpIdsApChecksIntDao erpIdsApChecksIntDao;
    @Resource
    private ErpIdsArReceiptsIntDao erpIdsArReceiptsIntDao;
    @Resource
    private ErpIdsArReceiptAppliedIntDao erpIdsArReceiptAppliedIntDao;
    @Resource
    private ErpIdsApConReceiptsIntDao erpIdsApConReceiptsIntDao;
    @Resource
    private ErpIdsControlDao erpIdsControlDao;

    //批量写入数据暂未控制批次大小，通常情况不会超过数据库限制的最大批量
    // 碰到超限了再修改
    @Override
    public String putErpData(String serviceID,
                             String token,
                             String transactionID,
                             String pkg,
                             String totalPackage,
                             String rollback,
                             String forceReplace,
                             String data) {

        LOGGER.info("erpput():request-{serviceID:{},token:{},transationID:{},"
                        + "package:{},totalPackage:{},rollback:{},forceReplace:{},data:{}}",
                serviceID, token, transactionID, pkg, totalPackage, rollback, forceReplace, data);

        if (!validate(serviceID, token, transactionID)) {
            return errorMsg(serviceID, token, transactionID, errorCode, errorMsg);
        }
        if (data == null || "".equals(data.trim())) {
            return noData(serviceID, token, transactionID);
        }
        long controlId = 0;
        try {
            data = java.net.URLDecoder.decode(data, "utf-8");
        } catch (Exception e) {
            LOGGER.info("data URLDecode incorrect,don't need  URLDecode");
        } finally {
            try {
                XStream xstream = new XStream(new DomDriver());
                //注册日期转换，处理 yyyy-MM-dd HH:mm:ss, yyyy-MM-dd格式日期
                xstream.registerConverter(new DateConverter("yyyy-MM-dd HH:mm:ss", null, TimeZone.getTimeZone("GMT+8")));
                xstream.alias("ErpIdsControl", ErpIdsControl.class);
                if ("erp_ids_contract".equals(serviceID)) { //合同数据
                    xstream.alias("ErpIdsApContractInt", ErpIdsApContractInt.class);
                } else if ("erp_ids_payments".equals(serviceID)) { //付款数据
                    xstream.alias("ErpIdsApConPaymentsInt", ErpIdsApConPaymentsInt.class);
                } else if ("ap_invoice".equals(serviceID)) { //应付数据
                    xstream.alias("ErpIdsApInvoice", ErpIdsApInvoice.class);
                    xstream.alias("ErpIdsApInvoiceLine", ErpIdsApInvoiceLine.class);
                } else if ("inv_ar".equals(serviceID)) {
                    xstream.alias("ErpIdsInvAr", ErpIdsInvAr.class);
                } else if ("inv_ap".equals(serviceID)) {
                    xstream.alias("ErpIdsInvAp", ErpIdsInvAp.class);
                    xstream.alias("ErpIdsInvApLine", ErpIdsInvApLine.class);
                } else if ("ar_customers".equals(serviceID)) {
                    xstream.alias("ErpIdsArCustomers", ErpIdsArCustomers.class);
                } else if ("general_ledger".equals(serviceID)) {
                    xstream.alias("ErpIdsGLInt", ErpIdsGLInt.class);
                } else if ("ar_invoice".equals(serviceID)) {
                    xstream.alias("ErpIdsArInvoicesInt", ErpIdsArInvoicesInt.class);
                } else if ("ap_checks".equals(serviceID)) {
                    xstream.alias("ErpIdsApChecksInt", ErpIdsApChecksInt.class);
                } else if ("ar_receipts".equals(serviceID)) {
                    xstream.alias("ErpIdsArReceiptsInt", ErpIdsArReceiptsInt.class);
                    xstream.alias("ErpIdsArReceiptAppliedInt", ErpIdsArReceiptAppliedInt.class);
                } else if ("ap_receipts".equals(serviceID)) {
                    xstream.alias("ErpIdsApConReceiptsInt", ErpIdsApConReceiptsInt.class);
                }

                List<Object> listData = (List<Object>) xstream.fromXML(data);
                controlId = checkDataValid(listData);
                if (controlId == -1)
                    return errorMsg(serviceID, token, transactionID, errorCode, errorMsg);
                int listDataCount = listData.size();

                if ("erp_ids_contract".equals(serviceID)) {   //合同数据
                    List<ErpIdsApContractInt> erpIdsApContractIntList = new ArrayList<ErpIdsApContractInt>();
                    for (int i = 1; i < listDataCount; i++) {
                        ErpIdsApContractInt erpIdsApContractInt = (ErpIdsApContractInt) listData.get(i);
                        if (erpIdsApContractInt.getControlId() == controlId) { //检查批次controlId
                            erpIdsApContractIntList.add(erpIdsApContractInt);
                        }
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsApContractInt>> listsErpIdsApContractIntInSameSize = Lists.partition(erpIdsApContractIntList, batchSize);
                    for (List<ErpIdsApContractInt> item : listsErpIdsApContractIntInSameSize) {
                        erpIdsApContractIntDao.batchInsertErpIdsApContractIntData(item);
                    }
                } else if ("erp_ids_payments".equals(serviceID)) {   //付款数据
                    List<ErpIdsApConPaymentsInt> erpIdsApConPaymentsInts = new ArrayList<ErpIdsApConPaymentsInt>();
                    for (int i = 1; i < listDataCount; i++) {
                        ErpIdsApConPaymentsInt erpIdsApConPaymentsInt = (ErpIdsApConPaymentsInt) listData.get(i);
                        if (erpIdsApConPaymentsInt.getControlId() == controlId) { //检查批次controlId
                            erpIdsApConPaymentsInts.add(erpIdsApConPaymentsInt);
                        }
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsApConPaymentsInt>> listsErpIdsApConPaymentsIntInSameSize = Lists.partition(erpIdsApConPaymentsInts, batchSize);
                    for (List<ErpIdsApConPaymentsInt> item : listsErpIdsApConPaymentsIntInSameSize) {
                        erpIdsApConPaymentsIntDao.batchInsertErpIdsApConPaymentsIntData(item);
                    }
                } else if ("ap_invoice".equals(serviceID)) {  //应付数据
                    List<ErpIdsApInvoiceLine> erpIdsApInvoiceLineArrayList = new ArrayList<ErpIdsApInvoiceLine>();
                    List<ErpIdsApInvoice> erpIdsApInvoiceList = new ArrayList<ErpIdsApInvoice>();
                    for (int i = 1; i < listDataCount; i++) {
                        Object putDataItem = listData.get(i);
                        if (putDataItem instanceof ErpIdsApInvoiceLine) { //应付明细
                            ErpIdsApInvoiceLine erpIdsApInvoiceLine = (ErpIdsApInvoiceLine) putDataItem;
                            if (erpIdsApInvoiceLine.getControlId() == controlId) {
                                erpIdsApInvoiceLineArrayList.add(erpIdsApInvoiceLine);
                            }
                        } else if (putDataItem instanceof ErpIdsApInvoice) { //应付
                            ErpIdsApInvoice erpIdsApInvoice = (ErpIdsApInvoice) putDataItem;
                            if (erpIdsApInvoice.getControlId() == controlId) {  //检查批次controlId
                                erpIdsApInvoiceList.add(erpIdsApInvoice);
                            }
                        }
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsApInvoiceLine>> listsErpIdsApInvoiceLineInSameSize = Lists.partition(erpIdsApInvoiceLineArrayList, batchSize);
                    for (List<ErpIdsApInvoiceLine> item : listsErpIdsApInvoiceLineInSameSize) {
                        erpApInvoiceLineDao.batchInsertErpIdsApInvoiceLineData(item);
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsApInvoice>> listsErpIdsApInvoiceInSameSize = Lists.partition(erpIdsApInvoiceList, batchSize);
                    for (List<ErpIdsApInvoice> item : listsErpIdsApInvoiceInSameSize) {
                        erpApInvoiceDao.batchInsertErpIdsApInvoiceData(item);
                    }
                } else if ("inv_ar".equals(serviceID)) {   //应收数据-数量账
                    List<ErpIdsInvAr> erpIdsInvArList = new ArrayList<ErpIdsInvAr>();
                    for (int i = 1; i < listDataCount; i++) {
                        ErpIdsInvAr erpIdsInvAr = (ErpIdsInvAr) listData.get(i);
                        if (erpIdsInvAr.getControlId() == controlId) { //检查批次controlId
                            erpIdsInvArList.add(erpIdsInvAr);
                        }
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsInvAr>> listsErpIdsInvArInSameSize = Lists.partition(erpIdsInvArList, batchSize);
                    for (List<ErpIdsInvAr> item : listsErpIdsInvArInSameSize) {
                        erpIdsInvArDao.batchInsertInvArData(item);
                    }
                } else if ("inv_ap".equals(serviceID)) {   //应付数据-数量账
                    List<ErpIdsInvApLine> erpIdsInvApLineArrayList = new ArrayList<ErpIdsInvApLine>();
                    List<ErpIdsInvAp> erpIdsInvApList = new ArrayList<ErpIdsInvAp>();
                    for (int i = 1; i < listDataCount; i++) {
                        Object putDataItem = listData.get(i);
                        if (putDataItem instanceof ErpIdsInvApLine) { //数量帐-应付明细
                            ErpIdsInvApLine erpIdsInvApLine = (ErpIdsInvApLine) putDataItem;
                            if (erpIdsInvApLine.getControlId() == controlId) {
                                erpIdsInvApLineArrayList.add(erpIdsInvApLine);
                            }
                        } else if (putDataItem instanceof ErpIdsInvAp) { //数量帐-应付
                            ErpIdsInvAp erpIdsInvAp = (ErpIdsInvAp) putDataItem;
                            if (erpIdsInvAp.getControlId() == controlId) {  //检查批次controlId
                                erpIdsInvApList.add(erpIdsInvAp);
                            }
                        }
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsInvApLine>> listsErpIdsInvApLineInSameSize = Lists.partition(erpIdsInvApLineArrayList, batchSize);
                    for (List<ErpIdsInvApLine> item : listsErpIdsInvApLineInSameSize) {
                        erpIdsInvApLineDao.batchInsertErpIdsInvApLineData(item);
                    }
                    List<List<ErpIdsInvAp>> listsErpIdsInvApInSameSize = Lists.partition(erpIdsInvApList, batchSize);
                    for (List<ErpIdsInvAp> item : listsErpIdsInvApInSameSize) {
                        erpIdsInvApDao.batchInsertInvApData(item);
                    }
                } else if ("ar_invoice".equals(serviceID)) {   //发票
                    List<ErpIdsArInvoicesInt> erpIdsArInvoicesIntList = new ArrayList<ErpIdsArInvoicesInt>();
                    for (int i = 1; i < listDataCount; i++) {
                        ErpIdsArInvoicesInt erpIdsArInvoicesInt = (ErpIdsArInvoicesInt) listData.get(i);
                        if (erpIdsArInvoicesInt.getControlId() == controlId) {  //检查批次controlId
                            erpIdsArInvoicesIntList.add(erpIdsArInvoicesInt);
                        }
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsArInvoicesInt>> listsErpIdsArInvoicesIntInSameSize = Lists.partition(erpIdsArInvoicesIntList, batchSize);
                    for (List<ErpIdsArInvoicesInt> item : listsErpIdsArInvoicesIntInSameSize) {
                        erpArInvoiceIntDao.batchInsertErpIdsArInvoicesIntData(item);
                    }
                } else if ("ar_customers".equals(serviceID)) {  //客户基本资料
                    List<ErpIdsArCustomers> erpIdsArCustomersList = new ArrayList<ErpIdsArCustomers>();
                    for (int i = 1; i < listDataCount; i++) {
                        ErpIdsArCustomers erpIdsArCustomers = (ErpIdsArCustomers) listData.get(i);
                        if (erpIdsArCustomers.getControlId() == controlId) //检查批次controlId
                            erpIdsArCustomersList.add(erpIdsArCustomers);
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsArCustomers>> listsErpIdsArCustomersInSameSize = Lists.partition(erpIdsArCustomersList, batchSize);
                    for (List<ErpIdsArCustomers> item : listsErpIdsArCustomersInSameSize) {
                        erpIdsArCustomersDao.batchInsertErpIdsArCustomersData(item);
                    }
                } else if ("general_ledger".equals(serviceID)) { //ERP总账数据
                    List<ErpIdsGLInt> erpIdsGLIntList = new ArrayList<ErpIdsGLInt>();
                    for (int i = 1; i < listDataCount; i++) {
                        ErpIdsGLInt erpIdsGLInt = (ErpIdsGLInt) listData.get(i);
                        if (erpIdsGLInt.getControlId() == controlId) //检查批次controlId
                            erpIdsGLIntList.add(erpIdsGLInt);
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsGLInt>> listsErpIdsGLIntInSameSize = Lists.partition(erpIdsGLIntList, batchSize);
                    for (List<ErpIdsGLInt> item : listsErpIdsGLIntInSameSize) {
                        erpIdsGLIntDao.batchInsertErpIdsGLIntData(item);
                    }
                } else if ("ap_checks".equals(serviceID)) {   //付款
                    List<ErpIdsApChecksInt> erpIdsApChecksIntList = new ArrayList<ErpIdsApChecksInt>();
                    for (int i = 1; i < listDataCount; i++) {
                        ErpIdsApChecksInt erpIdsApChecksInt = (ErpIdsApChecksInt) listData.get(i);
                        if (erpIdsApChecksInt.getControlId() == controlId) { //检查批次controlId
                            erpIdsApChecksIntList.add(erpIdsApChecksInt);
                        }
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsApChecksInt>> listsErpIdsApChecksIntInSameSize = Lists.partition(erpIdsApChecksIntList, batchSize);
                    for (List<ErpIdsApChecksInt> item : listsErpIdsApChecksIntInSameSize) {
                        erpIdsApChecksIntDao.batchInsertErpIdsApChecksIntData(item);
                    }
                } else if ("ar_receipts".equals(serviceID)) {  //收款
                    List<ErpIdsArReceiptsInt> erpIdsArReceiptsIntList = new ArrayList<ErpIdsArReceiptsInt>();
                    List<ErpIdsArReceiptAppliedInt> erpIdsArReceiptAppliedIntList = new ArrayList<ErpIdsArReceiptAppliedInt>();
                    for (int i = 1; i < listDataCount; i++) {
                        Object putDataItem = listData.get(i);
                        if (putDataItem instanceof ErpIdsArReceiptsInt) { //收款
                            ErpIdsArReceiptsInt erpIdsArReceiptsInt = (ErpIdsArReceiptsInt) putDataItem;
                            if (erpIdsArReceiptsInt.getControlId() == controlId) {  //检查批次controlId
                                erpIdsArReceiptsIntList.add(erpIdsArReceiptsInt);
                            }
                        } else if (putDataItem instanceof ErpIdsArReceiptAppliedInt) { //收款明细
                            ErpIdsArReceiptAppliedInt erpIdsArReceiptAppliedInt = (ErpIdsArReceiptAppliedInt) putDataItem;
                            if (erpIdsArReceiptAppliedInt.getControlId() == controlId) {    //检查批次controlId
                                erpIdsArReceiptAppliedIntList.add(erpIdsArReceiptAppliedInt);
                            }
                        }
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsArReceiptsInt>> listsErpIdsArReceiptsIntInSameSize = Lists.partition(erpIdsArReceiptsIntList, batchSize);
                    for (List<ErpIdsArReceiptsInt> item : listsErpIdsArReceiptsIntInSameSize) {
                        erpIdsArReceiptsIntDao.batchInsertErpIdsArReceiptsIntData(item);
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsArReceiptAppliedInt>> listsErpIdsArReceiptAppliedIntInSameSize = Lists.partition(erpIdsArReceiptAppliedIntList, batchSize);
                    for (List<ErpIdsArReceiptAppliedInt> item : listsErpIdsArReceiptAppliedIntInSameSize) {
                        erpIdsArReceiptAppliedIntDao.batchInsertErpIdsArReceiptAppliedIntData(item);
                    }
                } else if ("ap_receipts".equals(serviceID)) {   //收款信息
                    List<ErpIdsApConReceiptsInt> erpIdsApConReceiptsIntList = new ArrayList<ErpIdsApConReceiptsInt>();
                    for (int i = 1; i < listDataCount; i++) {
                        ErpIdsApConReceiptsInt erpIdsApConReceiptsInt = (ErpIdsApConReceiptsInt) listData.get(i);
                        if (erpIdsApConReceiptsInt.getControlId() == controlId) { //检查批次controlId
                            erpIdsApConReceiptsIntList.add(erpIdsApConReceiptsInt);
                        }
                    }
                    //分离数组进行批量插入
                    List<List<ErpIdsApConReceiptsInt>> listsErpIdsApChecksIntInSameSize = Lists.partition(erpIdsApConReceiptsIntList, batchSize);
                    for (List<ErpIdsApConReceiptsInt> item : listsErpIdsApChecksIntInSameSize) {
                        erpIdsApConReceiptsIntDao.batchInsertErpIdsApConReceiptsIntData(item);
                    }
                }
                ErpIdsControl erpIdsControl = (ErpIdsControl) listData.get(0);
                erpIdsControlDao.insertSelective(erpIdsControl);
            } catch (Exception ex) {
                LOGGER.error("ERPPutServiceImpl error Message:{}", ex.getMessage());
                //推送数据异常，回滚
                try {
                    if ("erp_ids_contract".equals(serviceID)) {
                        erpIdsApContractIntDao.deleteErpIdsApContractIntByControlId(controlId);
                    } else if ("erp_ids_payments".equals(serviceID)) {
                        erpIdsApConPaymentsIntDao.deleteErpIdsApConPaymentsIntByControlId(controlId);
                    } else if ("ap_invoice".equals(serviceID)) {
                        erpApInvoiceDao.deleteApInvoiceByControlId(controlId);
                        erpApInvoiceLineDao.deleteApInvoiceLineByControlId(controlId);
                    } else if ("inv_ar".equals(serviceID)) {
                        erpIdsInvArDao.deleteInvArByControlId(controlId);
                    } else if ("inv_ap".equals(serviceID)) {
                        erpIdsInvApLineDao.deleteInvApLineByControlId(controlId);
                        erpIdsInvApDao.deleteInvApByControlId(controlId);
                    } else if ("ar_invoice".equals(serviceID)) {
                        erpArInvoiceIntDao.deleteArInvoicesIntByControlId(controlId);
                    } else if ("general_ledger".equals(serviceID)) {
                        erpIdsGLIntDao.deleteGLIntByControlId(controlId);
                    } else if ("ap_checks".equals(serviceID)) {
                        erpIdsApChecksIntDao.deleteApChecksIntByControlId(controlId);
                    } else if ("ar_receipts".equals(serviceID)) {
                        erpIdsArReceiptsIntDao.deleteArReceiptsIntByControlId(controlId);
                        erpIdsArReceiptAppliedIntDao.deleteArReceiptAppliedIntByControlId(controlId);
                    } else if ("ap_receipts".equals(serviceID)) {
                        erpIdsApConReceiptsIntDao.deleteErpIdsApConReceiptsIntByControlId(controlId);
                    }
                } catch (Exception rollbackException) {
                    LOGGER.error("ERPPutServiceImpl  Rollback Error Message:{}", rollbackException.getMessage());
                }
                return errorMsg(serviceID, token, transactionID, Constants.CODE_ERROR, ex.getMessage());
            }
            LOGGER.info("erpput success :request-{serviceID:{},token:{},data:{}}", serviceID, token, data);
            return wrap2XML(serviceID, token, transactionID, Constants.CODE_SUCCESS, Constants.CODE_SUCCESS_MSG, "1", "1");
        }
    }

    private boolean validate(String serviceID, String token, String transactionID) {
        boolean isValid = true;
        String[] userInfo = securtiyChecker.getUserInfo(token);// 验证token

        if (userInfo == null) {
            errorCode = Constants.CODE_AUTH_ERROR;
            errorMsg = "没有访问的权限";
            isValid = false;
        }

        //检查serviceID
        if (StringUtil.isEmpty(serviceID)) {
            errorCode = Constants.CODE_CLIENT_ERROR;
            errorMsg = "serviceID can not be null";
            isValid = false;
        } else {
            if (!servicIDs.contains(serviceID)) {
                errorCode = Constants.CODE_CLIENT_ERROR;
                errorMsg = "illegal serviceID";
                isValid = false;
            }
        }
        return isValid;
    }

    private long checkDataValid(List<Object> listData) {
        ErpIdsControl erpIdsControl = (ErpIdsControl) listData.get(0);
        if (erpIdsControl == null || erpIdsControl.getControlId() == 0) {
            errorCode = Constants.CODE_CLIENT_ERROR;
            errorMsg = "illegal ControlId";
            return -1;
        }
        return erpIdsControl.getControlId();
    }

    /**
     * 返回错误信息
     *
     * @param serviceID
     * @param token
     * @param transactionID
     * @param result        错误结果
     * @param msg           错误信息详情
     * @return
     */
    private static String errorMsg(String serviceID, String token,
                                   String transactionID, String result, String msg) {
        StringBuilder sb = new StringBuilder();
        LOGGER.info("erpput error :request-{serviceID:{},errorMsg:{}}", serviceID, msg);
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><service>")
                .append("<serviceID>").append(serviceID).append("</serviceID>")
                .append("<token>").append(token).append("</token>")
                .append("<transactionID>").append(transactionID)
                .append("</transactionID>").append("<result>").append(result)
                .append("</result><msg>").append(msg)
                .append("</msg></service>");
        return sb.toString();
    }

    /**
     * 返回客户端无数据内容
     *
     * @return
     */
    private static String noData(String serviceID, String token, String transactionID) {
        return errorMsg(serviceID, token, transactionID, Constants.CODE_CLIENT_ERROR, "发送无数据内容!");
    }

    private String wrap2XML(String serviceID, String token,
                            String transationID, String result, String msg, String received,
                            String submitted) {
        Document doc = DocumentHelper.createDocument();
        Element serviceElem = doc.addElement("service");
        try {
            Element idElem = serviceElem.addElement("serviceID");
            idElem.addText(serviceID);

            Element tokenElem = serviceElem.addElement("token");
            tokenElem.addText(token);

            Element transtationElem = serviceElem.addElement("transactionID");
            transtationElem.addText(transationID == null ? "" : transationID);

            Element resultElem = serviceElem.addElement("result");
            resultElem.addText(result);

            Element msgElem = serviceElem.addElement("msg");
            msgElem.addText(msg);

            Element receivedElem = serviceElem.addElement("received");
            receivedElem.addText(received);

            Element submittedElem = serviceElem.addElement("submitted");
            submittedElem.addText(submitted);
        } catch (Exception e) {
            LOGGER.error("wrap2XML():e-{}", e.getMessage());
            return errorMsg(serviceID, token, transationID, Constants.CODE_SERVER_ERROR, "生成XML数据内容发生异常！");
        }
        return doc.asXML();
    }
}
